<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

class Model extends CI_Model {
    
    public function __construct() {
        $this->load->database();
    }
    
    public function get_news($slug = FALSE) {
        if ($slug === FALSE) {
            $query = $this->db->get('news');
            return $query->result_array();
        }

        $query = $this->db->get_where('news', array('slug' => $slug));
        return $query->row_array();
    }
    
    public function set_news() {
        $this->load->helper('url');

        $slug = url_title($this->input->post('title'), 'dash', TRUE);
        //$cover = $this->input->post('cover_image');
        
        $data = array(
            'title' => $this->input->post('title'),
            'slug' => $slug,
            'text' => $this->input->post('text'),
            
        );

        return $this->db->insert('news', $data);
    }
    
    public function set_crop() {
        $this->load->helper('url');
        $this->load->library('image_lib');
        $x = $this->input->post('x');
        $y = $this->input->post('y');
        $width = $this->input->post('width');
        $height = $this->input->post('height');
        //$imagePath = $_SERVER['DOCUMENT_ROOT']. '/pruebaCodeIgniter/assets/img/wonder.jpg';
        //$imageDest = $_SERVER['DOCUMENT_ROOT']. '/pruebaCodeIgniter/uploads/wonderCrop.jpg';
        /*
         * Para utilizar las librerias de imagemagick y netpbm se ha de especificar el path
         * imageMagickConvert = /usr/bin/convert 
         * imageMagickComposite = /usr/bin/composite
         * http://stackoverflow.com/questions/16161102/how-can-i-provide-image-library-path-for-imagemagick 
         */
        $config['image_library'] = 'gd2';
        $config['source_image'] = 'assets/img/wonder.jpg';
        $config['new_image'] = 'uploads/wonderCrop.jpg';
        $config['x_axis'] = intval($x);
        $config['y_axis'] = intval($y);
        $config['width'] = intval($width);
        $config['height'] = intval($height);
        $this->image_lib->initialize($config);
        if ( ! $this->image_lib->crop())
        {
            echo $this->image_lib->display_errors();
        }
        /*$imagick = new Imagick($imagePath);
        $imagick->cropImage(intval($width), intval($height), intval($x), intval($y));
        $imagick->writeImage($imageDest);*/
        
    }
    
    public function check_user($user) {
        $query = $this->db->get_where('users', array('user_name' => $user));
        return $query->row_array();
    }
}